package com.yangwudi.controller;


import com.yangwudi.anno.LogOperation;
import com.yangwudi.pojo.PageResult;
import com.yangwudi.pojo.Result;
import com.yangwudi.pojo.Student;
import com.yangwudi.pojo.StudentQueryParam;
import com.yangwudi.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/students")
@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 学员列表-条件分页查询
     */
    @GetMapping
    public Result page(StudentQueryParam studentQueryParam){
        log.info("分页查询学员数据:{}",studentQueryParam);
        PageResult<Student> pageResult = studentService.page(studentQueryParam);
        return Result.success(pageResult);
    }


    /**
     * 根据ID批量删除学员信息
     */
    @LogOperation
    @DeleteMapping("/{ids}")
    public Result deleteByIds(@PathVariable List<Integer> ids){
        log.info("删除学员: {}", ids);
        studentService.deleteByIds(ids);
        return Result.success();
    }


    /**
     * 添加学员
     */
    @LogOperation
    @PostMapping
    public Result addStudent(@RequestBody Student student){
        log.info("添加学员: {}", student);
        studentService.addStudent(student);
        return Result.success();
    }

    /**
     * 查询回显 - 根据主键ID查询学员的信息
     */

    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){
        log.info("根据主键ID查询学员的信息:{}",id);
        Student student = studentService.getById(id);
        return Result.success(student);
    }

    /**
     * 修改学员信息
     */
    @LogOperation
    @PutMapping
    public Result update(@RequestBody Student student){
        log.info("修改学员信息:{}",student);
        studentService.update(student);
        return Result.success();
    }


    /**
     * 违纪处理
     */
    @PutMapping("/violation/{id}/{score}")
    public Result handleViolation(@PathVariable Integer id, @PathVariable("score") Integer violationScore){
        log.info("违纪处理:学员ID{},扣除分数{}",id,violationScore);
        studentService.handleViolation(id,violationScore);
        return Result.success();

    }
}
